﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using PSE.Framework.ErrorLogging;

namespace PSE.Updater.Common.Configurations.Server
{
    public class ProductConfigurationElement : ConfigurationElement
    {
        /// <summary>
        /// Nome do Produto
        /// </summary>
        [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        /// <summary>
        /// Lista de Observers configurados
        /// </summary>
        [ConfigurationProperty("providers", IsDefaultCollection = true)]
        public ProviderConfigurationElementCollection Providers
        {
            get
            {
                ProviderConfigurationElementCollection provider = (ProviderConfigurationElementCollection)base["providers"];
                FormatProductsDirectories(ref provider);
                return provider;
            }
        }

        private void FormatProductsDirectories(ref ProviderConfigurationElementCollection reference)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.Name))
                {
                    //string installedPath = PSE.Updater.Common.Util.Instance.GetInstallationPath(this.Name);
                    //installedPath = installedPath.Replace(this.Name, string.Empty);
                    foreach (ProviderConfigurationElement provider in reference)
                    {
                        foreach (WatcherConfigurationElement watchers in provider.Watchers)
                        {
                            //watchers.DirectoryTemplate = string.Format(watchers.Directory, installedPath);
                            watchers.DirectoryTemplate = watchers.Directory;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog(LogType.Warning, ex);
            }
        }
    }
}
